package com.seedwill.common.query.filter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.seedwill.common.query.ScalarParaReg;

/**
 * APagedFilter
 * 
 * @author kdyi
 *
 */
public abstract class APagedFilter extends AFilter implements java.io.Serializable  {

	/**
	 * schema
	 */
	protected String schema;
	
	public String getSchema() {
		return schema;
	}
	public APagedFilter() {
		super();
	}
	public APagedFilter(String schema) {
		super();
		this.schema = schema;
	}
	/**
	 * 默认连接方式
	 */
	protected static String defaultJoin = "and";
	
	private int startAtIndex = -1;

    private int count = -1;

    /**
     * @return Returns the startAtIndex.
     */
    public int getStartAtIndex() {
        return startAtIndex;
    }

    /**
     * @param startAtIndex The startAtIndex to set.
     */
    public void setStartAtIndex(int startAtIndex) {
        this.startAtIndex = startAtIndex;
    }
    
    private int limitMaxIndex;
    
    public int getLimitMaxIndex() {
		return limitMaxIndex;
	}
	public void setLimitMaxIndex(int limitMaxIndex) {
		this.limitMaxIndex = limitMaxIndex;
	}
	/**
     * @return Returns the endAtIndex.
     */
    public int getCount() {
        return count;
    }

    /**
     * @param count The endAtIndex to set.
     */
    public void setCount(int count) {
        this.count = count;
    }
	
	public void addBetweenJoined(String joinFlag, String field, Object low, Object high) {
		Expression exp = Expression.between(field, low, high);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addClauseJoined(String joinFlag, String clause, Object... objects) {
		Expression exp = Expression.sqlClause(clause, objects);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addEqualToJoined(String joinFlag, String field, Object value) {
		Expression exp = Expression.equal(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addGreaterOrEqualThanJoined(String joinFlag, String field, Object value) {
		Expression exp = Expression.greaterOrEqual(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addGreaterThanJoined(String joinFlag, String field, Object value) {
		Expression exp = Expression.greaterThan(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addInJoined(String joinFlag, String field, Collection value) {
		Expression exp = Expression.in(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addIsNullJoined(String joinFlag, String field) {
		Expression exp = Expression.nullField(field, null);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addLeftLikeJoined(String joinFlag, String filed, Object value) {
		Expression exp = Expression.leftLike(filed, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addLessOrEqualThanJoined(String joinFlag, String field, Object value) {
		Expression exp = Expression.lessOrEqual(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addLessThanJoined(String joinFlag, String field, Object value) {
		Expression exp = Expression.lessThan(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addLikeJoined(String joinFlag, String field, String value) {
		Expression exp = Expression.like(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addNotBetweenJoined(String joinFlag, String field, Object low, Object high) {
		Expression exp = Expression.notBetween(field, low, high);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}


	public void addNotEqualToJoined(String joinFlag, String field, Object value) {
		Expression exp = Expression.notEqual(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}


	public void addNotInJoined(String joinFlag, String field, Collection value) {
		Expression exp = Expression.equal(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addNotLeftLikeJoined(String joinFlag, String field, Object value) {
		Expression exp = Expression.notLeftLike(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}


	public void addNotLikeJoined(String joinFlag, String field, String value) {
		Expression exp = Expression.notLike(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}


	public void addNotNullJoined(String joinFlag, String field) {
		Expression exp = Expression.notNullField(field, field);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addNotRightLikeJoined(String joinFlag, String field, Object value) {
		Expression exp = Expression.notRightLike(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}

	public void addRightLikeJoined(String joinFlag, String field, Object value) {
		Expression exp = Expression.rightLike(field, value);
		exp.setJoin(joinFlag);
		filters.add(exp);
	}
	
	
	public void addBetween( String field, Object low, Object high) {
		Expression exp = Expression.between(field, low, high);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}


	public void addClause( String hql, Object... objects) {
		Expression exp = Expression.clause(hql, objects);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}


	public void addEqualTo( String field, Object value) {
		Expression exp = Expression.equal(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	public void addGreaterOrEqualThan( String field, Object value) {
		Expression exp = Expression.greaterOrEqual(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}


	public void addGreaterThan( String field, Object value) {
		Expression exp = Expression.greaterThan(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}


	public void addIn( String field, Collection value) {
		Expression exp = Expression.in(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}


	public void addIsNull( String field) {
		Expression exp = Expression.nullField(field, null);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	public void addLessOrEqualThan( String field, Object value) {
		Expression exp = Expression.lessOrEqual(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	public void addLessThan( String field, Object value) {
		Expression exp = Expression.lessThan(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}


	public void addLike( String field, String value) {
		Expression exp = Expression.like(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	/**
	 * @param clause
	 * @param objects
	 */
	public void addNativeClauseDefault(String clause, Object... objects) {
		Expression exp = Expression.sqlClause(clause, objects);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	public void addNotBetween( String field, Object low, Object high) {
		Expression exp = Expression.notBetween(field, low, high);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	public void addNotEqualTo( String field, Object value) {
		Expression exp = Expression.notEqual(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	public void addNotIn( String field, Collection value) {
		Expression exp = Expression.equal(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	/**
	 * @param field
	 * @param value
	 */
	public void addNotLeftLikeDefault(String field, Object value) {
		Expression exp = Expression.notLeftLike(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	/**
	 * @param field
	 * @param value
	 */
	public void addNotLikeDefault( String field, String value) {
		Expression exp = Expression.notLike(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	/* (non-Javadoc)
	 * @see cn.gov.zjport.persistence.core.filter.base.Filter#addNotNull(java.lang.String)
	 */
	public void addNotNull( String field) {
		Expression exp = Expression.notNullField(field, field);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	/**
	 * @param field
	 * @param value
	 */
	public void addNotRightLikeDefault( String field, Object value) {
		Expression exp = Expression.notRightLike(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	/**
	 * @param field
	 * @param value
	 */
	public void addRightLikeDefault(String field, Object value) {
		Expression exp = Expression.rightLike(field, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}

	/**
	 * 参数
	 */
	protected Object[] sqlPara;
	
	/**
	 * @return
	 */
	public Object[] getSqlPara() {
		return sqlPara;
	}

	/**
	 * @param sqlPara
	 */
	public void setSqlPara(Object[] sqlPara) {
		this.sqlPara = sqlPara;
	}
	
	/**
	 * 
	 */
	protected List<ScalarParaReg> sqlScalarParaReg;
	
	public List<ScalarParaReg> getSqlScalarParaReg() {
		return sqlScalarParaReg;
	}

	/**
	 * @param sqlScalarParaReg
	 */
	public void setSqlScalarParaReg(List<ScalarParaReg> sqlScalarParaReg) {
		this.sqlScalarParaReg = sqlScalarParaReg;
	}
	
	public void addScalarParaReg(ScalarParaReg p){
		if(sqlScalarParaReg == null){
			sqlScalarParaReg = new ArrayList<ScalarParaReg>();
		}
		sqlScalarParaReg.add(p);
	}
	
	public void removeScalarParaReg(ScalarParaReg p){
		sqlScalarParaReg.remove(p);
	}
	public void removeScalarParaReg(int pIdx){
		sqlScalarParaReg.remove(pIdx);
	}
	public void cleanScalarParaReg(){
		sqlScalarParaReg.clear();
	}
		
	public void addLeftLikeDefault(String filed, Object value) {
		Expression exp = Expression.leftLike(filed, value);
		exp.setJoin(defaultJoin);
		filters.add(exp);
	}
	
	/**
	 * 增加 like %aa 查询条件
	 * 
	 * @param filed
	 * @param value
	 */
	public void addLeftLike(String filed, Object value ){
		//Filter
		filters.add(Expression.leftLike(filed, value));
	}
	/**
	 * 增加 not like %aa 查询条件
	 * 
	 * @param filed
	 * @param value
	 */
	public void addNotLeftLike(String filed, Object value ){
		//Filter
		filters.add(Expression.notLeftLike(filed, value));
	}
	/**
	 * 增加 like aa% 查询条件
	 * 
	 * @param filed
	 * @param value
	 */
	public void addRightLike(String filed, Object value ){
		//Filter
		filters.add(Expression.rightLike(filed, value));
	}
	/**
	 * 增加 not like aa% 查询条件
	 * 
	 * @param filed
	 * @param value
	 */
	public void addNotRightLike(String filed, Object value ){
		//Filter
		filters.add(Expression.notRightLike(filed, value));
	}
	/**
	 * 增加附加查询语句到最终的查询语句末尾
	 * 
	 * @param clause
	 * @param value
	 */
	public void addQueryTailer(String clause, Object value ){
		filters.add(Expression.queryTailer(clause, value));
	}
}
